home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / awe2-0_1.lha / awe2-0.1 / Src / Semaphore.h < prev    next >
C/C++ Source or Header  |  1990-07-09  |  2KB  |  71 lines

  1. // This may look like C code, but it is really -*- C++ -*-
  2. // 
  3. // Copyright (C) 1988 University of Illinois, Urbana, Illinois
  4. // Copyright (C) 1989 University of Colorado, Boulder, Colorado
  5. // Copyright (C) 1990 University of Colorado, Boulder, Colorado
  6. //
  7. // written by Dirk Grunwald (grunwald@foobar.colorado.edu)
  8. //
  9. #ifndef SEMAPHOREH
  10. #define SEMAPHOREH
  11. #pragma once
  12.  
  13. //
  14. //    Semaphore.h
  15. //
  16. //    This implements a general counting semaphore. Each semaphore has
  17. //    a value known as 'count', which can be initialized to any value.
  18. //
  19. //    When count is < 1, and a process attempts to reserve the semaphore,
  20. //    that process is suspended. If count > 1, then the process continues.
  21. //    In either case, the count is decremented.
  22. //
  23. //    When releasing a semaphore, the count is incremented. If the count
  24. //    was less than 1, a single waiting process (if any is waiting) is
  25. //    allowed to proceed.
  26. // 
  27.  
  28. #include <ReserveByException.h>
  29. #include <SpinLock.h>
  30. #include <ThreadContainer.h>
  31.  
  32. class Semaphore : public ReserveByException {
  33.  
  34. protected:
  35.     SpinLock lock;
  36.     ThreadContainer *pScheduler;
  37.     int pCount;
  38.     char iDidAlloc;
  39.  
  40. private:
  41.     virtual int reserveByException( Thread *byWho );
  42.     
  43. public :
  44.  
  45.     Semaphore(int count = 1, ThreadContainer *scheduler = 0);
  46.     virtual ~Semaphore();
  47.  
  48.     virtual void reserve();
  49.     virtual void release();
  50.     virtual bool reserveNoBlock();
  51.  
  52.     virtual unsigned size();
  53.  
  54.     //
  55.     //    You should not change the count when threads are blocked
  56.     //
  57.     virtual int count();
  58.     virtual int count(int count);
  59.     virtual void incrCount(int increment);
  60.  
  61.     bool isEmpty();
  62. };
  63.  
  64. inline bool
  65. Semaphore::isEmpty()
  66. {
  67.     return( size() == 0);
  68. }
  69.  
  70. #endif SEMAPHOREH
  71.